

################
#Table 1
################

##read in Minozzi-Volden
MV_coding <- readRDS(file = paste0(getwd(), "/MVrollcalls.RDS"))

time.start <- proc.time()[3]

congs <- 101:111

##get vote types by leadership metric
all_table_data <- NULL
for (cong in congs){
  MVtmp <- subset(MV_coding, congress == cong)
  speechdir <- import(paste0(getwd(), "/Roll Call ",cong," House.xlsx"))
  speechdir$`Party Unity` <-  speechdir[,grep("Party Unity",colnames(speechdir))]
  speechdir$`Minozzi-Volden` <- 0
  for (j in 1:nrow(MVtmp)){
    speechdir$`Minozzi-Volden`[MVtmp$`Vote Number`[j] == speechdir$`Vote Number`] <- MVtmp$PartyCall[j]
  }
  keepthese <- c("Vote Number", "Pure procedural","Quasi-procedural","Amendment, recede/concur","Final Passage regular","Final Passage Suspension",
                 "Partisan Cohesiveness","Vote margin","Total votes","Dem leader speech","Rep leader speech",
                 "Dem party agenda vote","Rep party agenda vote","Party Unity","Party leadership vote", "Minozzi-Volden")
  tmp <- speechdir[,keepthese]
  tmp$Lopsided <- (tmp$`Vote margin`>30)*1
  rc_types <- c("Pure procedural","Quasi-procedural","Amendment, recede/concur","Final Passage regular","Final Passage Suspension")
  tmp$`RC Type` <- rc_types[1]
  for (k in 1:length(rc_types)){
    tmp$`RC Type`[tmp[,k]==1] <- rc_types[k] 
  }
  tmp$cong <- cong
  all_table_data <- rbind(all_table_data,tmp)
}

all_table_data$congress <- all_table_data$cong

##Now we need to calculate summary stats by vote type
leaderpostype1<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Party leadership vote`==1]))
leaderpostype2<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Party Unity`==1]))
leaderpostype3<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Lopsided`==0]))
leaderpostype4<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Rep party agenda vote`==1]))
leaderpostype5<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Dem party agenda vote`==1]))
leaderpostype6<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Rep leader speech`==1]))
leaderpostype7<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Dem leader speech`==1]))
leaderpostype8<- prop.table(table(all_table_data$`RC Type`[all_table_data$`Minozzi-Volden`==1]))

incidencetype1<- mean(all_table_data$`Party leadership vote`)
incidencetype2<- mean(all_table_data$`Party Unity`)
incidencetype3<- mean(all_table_data$`Lopsided`==0)
incidencetype5<- mean(all_table_data$`Dem party agenda vote`)
incidencetype4<- mean(all_table_data$`Rep party agenda vote`)
incidencetype6<- mean(all_table_data$`Rep leader speech`)
incidencetype7<- mean(all_table_data$`Dem leader speech`)
incidencetype8<- mean(all_table_data$`Minozzi-Volden`)

cohesivenesstype1<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Party leadership vote`==1])
cohesivenesstype2<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Party Unity`==1])
cohesivenesstype3<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Lopsided`==0])
cohesivenesstype4<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Rep party agenda vote`==1])
cohesivenesstype5<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Dem party agenda vote`==1])
cohesivenesstype6<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Rep leader speech`==1])
cohesivenesstype7<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Dem leader speech`==1])
cohesivenesstype8<- mean(all_table_data$`Partisan Cohesiveness`[all_table_data$`Minozzi-Volden`==1])


results <- t(rbind(leaderpostype1,leaderpostype2,leaderpostype3,leaderpostype4,leaderpostype5,leaderpostype6,leaderpostype7,leaderpostype8))
Incidence <- c(incidencetype1,incidencetype2,incidencetype3,incidencetype4,incidencetype5,incidencetype6,incidencetype7,incidencetype8)
Cohesiveness <- c(cohesivenesstype1,cohesivenesstype2,cohesivenesstype3,cohesivenesstype4,cohesivenesstype5,cohesivenesstype6,cohesivenesstype7,cohesivenesstype8)/100

results <- rbind(Incidence,results)
colnames(results) <- c("Party Leadership", "Party Unity", "Close", "Rep Party Agenda", 
                       "Dem Party Agenda", "Rep Leader Speech", "Dem Leader Speech", "Minozzi-Volden")
results <- round(results*100,digits=1)
for (i in 1:length(results)) results[i] <- paste(results[i],"%",sep="")

##print the table

fileConn<-file( paste0(getwd(), "/Table1.txt"))
writeLines(stargazer(results, type = "text"), fileConn)
close(fileConn)

################
#Table 2
################

##try to predict joining the FC and voting against Boehner

mod1 <- (glm(freedom_caucus ~ loyalty  ,
             data=thiscong, family = binomial, y=TRUE))

mod2 <- (glm(freedom_caucus ~ party_unity  ,
             data=thiscong, family = binomial, y=TRUE))

mod2a <- (glm(freedom_caucus ~ responsiveness_to_party_calls,
              data=thiscong, family = binomial, y=TRUE))

mod3 <- (glm(freedom_caucus ~ loyalty + ideals ,
             data=thiscong, family = binomial, y=TRUE))

mod4 <- (glm(freedom_caucus ~ party_unity + ideals  , 
             data=thiscong, family = binomial, y=TRUE))

mod4a <- (glm(freedom_caucus ~ responsiveness_to_party_calls + party_free_ideal_point  , 
              data=thiscong, family = binomial, y=TRUE))

mod5 <- (glm(vote_against_boehner ~ loyalty  ,
             data=thiscong, family = binomial, y=TRUE))

mod6 <- (glm(vote_against_boehner ~ party_unity  , 
             data=thiscong, family = binomial, y=TRUE))

mod6a <- (glm(vote_against_boehner ~ responsiveness_to_party_calls  , 
              data=thiscong, family = binomial, y=TRUE))

mod7 <- (glm(vote_against_boehner ~ loyalty + ideals  ,
             data=thiscong, family = binomial, y=TRUE))

mod8 <- (glm(vote_against_boehner ~ party_unity + ideals  , 
             data=thiscong, family = binomial, y=TRUE))

mod8a <- (glm(vote_against_boehner ~ responsiveness_to_party_calls + party_free_ideal_point  , 
              data=thiscong, family = binomial, y=TRUE))

#calculate errors
mod_list <- list(mod3,mod4,mod4a,mod7,mod8,mod8a)
model_errors <- rep(NA,length(mod_list))
num_defections <- rep(NA,length(mod_list))
for (i in 1:length(mod_list)) model_errors[i] <- sum((predict(mod_list[[i]],type="response")>0.5) != mod_list[[i]]$y)
for (i in 1:length(mod_list)) num_defections[i] <- sum((predict(mod_list[[i]],type="response")>0.5) ==1)
actual_votes <- c(rep(sum(thiscong$freedom_caucus),length(mod_list)/2), 
                  rep(sum(thiscong$vote_against_boehner),length(mod_list)/2))

##print the table and add the errors below

fileConn<-file( paste0(getwd(), "/Table2.txt"))
writeLines(
  screenreg(list(mod3,mod4,mod4a,mod7,mod8,mod8a),
            custom.coef.names = c("Intercept","Loyalty","Ideology","Unity","MV Loyalty","Ideology"),
            custom.gof.rows = list("Predicted Rebellion" = num_defections, 
                                   "Actual Rebellion" = actual_votes),
            stars = 0.05),
  fileConn)
close(fileConn)


################
#Table 3
################

##print the table

errtab <-   xtable(largest_errors[c("101-871", "102-789",
                                    "103-1094", "105-942",
                                    "107-872", "110-1053",
                                    "111-1084", "112-1168",
                                    "112-410"),])

fileConn<-file( paste0(getwd(), "/Table3.txt"))
writeLines(
  print(errtab), 
  fileConn)
close(fileConn)

